//2009/08/03 13:02:09
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <algorithm>

using namespace std;

class Gems
{
public:
    int numMoves(vector <string> board)
    {
        int count = 0;
        for (int i=0; i<board.size(); i++)
        {
            for (int j=0; j<board[i].size(); j++)
            {
                if (i<board.size()-1)
                {
                    swap(board[i][j],b[i+1][j]);
                    if (check(board))
                        count ++;
                    swap(board[i][j],b[i+1][j]);
                }
                if (j<board[i].size()-1)
                {
                    swap(board[i][j], b[i][j+1]);
                    if (check(board))
                        count ++;
                    swap(board[i][j], b[i][j+1]);
                }
            }
        }
        return count;
    }
private:
    bool check(vector<string>& b)
    {
        for (int i=0; i<b.size(); i++)
            for (int j=0; j<b[i].size()-2; j++)
                if (b[i][j] == b[i][j+1] && b[i][j] == b[i][j+2])
                    return true;
        for (int i=0; i<b[0].size(); i++)
            for (int j=0; j<b.size()-2; j++)
                if (b[j][i] == b[j+1][i] && b[j][i] == b[j+2][i])
                    return true;
        return false;
    }
    /*
    if (y == b[0].size()-1) return false;

    if (x>=2 && b[x][y+1] == b[x-1][y] && b[x-1][y] == b[x-2][y]) return true;
    if (x>=1 && x+1 <b.size() && b[x][y+1] == b[x-1][y] && b[x][y+1] == b[x+1][y]) return true;
    if (x+2<b.size() && b[x][y+1] == b[x+1][y] && b[x][y+1] == b[x+2][y]) return true;

    if (y>=2 && b[x][y+1] == b[x][y-2] && b[x][y+1] == b[x][y-1]) return true;
    if (y>=1 && b[x][y+1] == b[x][y] && b[x][y+1] == b[x][y-1]) return true;
    if (y+2 < b[0].size() && b[x][y] == b[x][y+1] && b[x][y] == b[x][y+2]) return true;
    return false;
    */

    //bool checkV(int x, int y, vector<string> b)
    //{
    /*
    if (x+1 == b.size()-1) return false;

    if(x>=2 && b[x+1][y] == b[x-1][y] && b[x+1][y] == b[x-2][y]) return true;
    if(x>=1 && b[x-1][y] == b[x+1][y] && b[x+1][y] == b[x][y]) return true;
    if(x+2 < b.size() && b[x+1][y] == b[x][y] && b[x][y] == b[x+2][y]) return true;

    if(y>=2 && b[x][y-2] == b[x][y-1] && b[x][y+1] == b[x+1][y]) return true;
    if(y>=1 && y+1<b[0].size() && b[x][y-1] == b[x+1][y] && b[x][y+1] == b[x+1][y]) return true;
    if(y+2<b[0].size() && b[x+1][y] == b[x][y+1] && b[x][y+1] == b[x][y+2]) return true;

    return false;
    */
    // }
};
